package com.doublefish.doublefishrobot.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.doublefish.doublefishrobot.R;

import java.util.List;

/**
 * 作者:dugaofeng on 2024/10/18 16:29
 */
public class ActionListDialog extends Dialog implements View.OnClickListener {

    private Context mContext;
    private String title;
//    private String msg;

    private ActionTitleAdapter adapter;
    private List<String> list;
    private OnActionListDialogListner onActionListDialogListner;

    private TextView titleTxt;
    RecyclerView recyclerView;
    private TextView cancelTxt;

    public ActionListDialog(Context context, String title, List<String> list, OnActionListDialogListner listener) {
        super(context,  R.style.dialog);

        this.mContext = context;
        this.title = title;
        this.list = list;
        this.onActionListDialogListner = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_action_list);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView() {
        titleTxt = (TextView) findViewById(R.id.title);
        titleTxt.setText(title);

        cancelTxt = (TextView) findViewById(R.id.cancel);
        cancelTxt.setOnClickListener(this);

        recyclerView = findViewById(R.id.recycler);

        adapter = new ActionTitleAdapter(list);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (onActionListDialogListner != null) {
                    onActionListDialogListner.onActionBtnClick(position, list.get(position));
                }
                dismiss();
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onClick(View view) {
        if (onActionListDialogListner != null) {
            onActionListDialogListner.onCancelBtnClick();
        }
        dismiss();
    }

    //region
    class ActionTitleAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

        public ActionTitleAdapter(List<String> list) {
            super(R.layout.item_dialog_action_list, list);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder baseViewHolder, String s) {
            baseViewHolder.setText(R.id.tv_name, s);
        }
    }

    //endregion


    //region
    public interface OnActionListDialogListner {
        void onActionBtnClick(int postion, String text);
        void onCancelBtnClick();
    }

    //endregion
}

